<?php

require_once('base.php');
require_once('file.php');
require_once $CFG->dirroot . 'mod/folio/view/file.php';
require_once $CFG->dirroot . 'mod/folio/model/artifact.php';

class folio_ArtifactView extends folio_BaseView {

    function __construct( folio_artifact $artifact ) {
        $a = new folio_ArtifactEditor( $artifact );
        $this->html = $a->html2( $artifact, $sStatus = 'normal', false );
    }

}

class folio_ArtifactEditor extends folio_BaseView {

    private $artifact;
    private $oHandle;
    private $oDelete;

    function __construct( folio_artifact $artifact ) {

        // Set labels
        $oHandle = __gettext('Click and drag this to move up or down');
        $oDelete = __gettext('Click to delete');

        $this->artifact = $artifact;
        $this->html = folio_ArtifactEditor::html2( $this->artifact);
        $this->js = <<< END
            folio_ajax_makeRichEditable('{$this->artifact->artifact_ident}', 'artifact', 'text', '{$this->artifact->handle()}_reflection');
END;

        // Load tooltips
        $this->Load(new folio_Tooltip('folio_clip_artifact' .
                                    $this->artifact->artifact_ident, $oHandle));
        $this->Load(new folio_Tooltip('folio_delete_artifact' .
                                    $this->artifact->artifact_ident, $oDelete));

    }

    // Display the html code for an item in the sortable (full) format.
    public function html2( folio_artifact $artifact, $sStatus = "normal", $sortable = true ) {
        global $CFG;
        global $FOLIO_CFG;
        $www = $CFG->wwwroot;
        $prefix = 'folio_artifact';
        $edit_link = $this->artifact->getLink('edit');
        $view_link = $this->artifact->getLink('view');


        // Allow collapsing via css w/o needing js initiation for each node
        if( $sStatus == 'normal' ) {
            $sStatus = 'style="display: none" ';
        } else {
            $sStatus = '';
        }

        // Create body text
        if ( $this->artifact->item_type == 'image' ) {

            $css_class = 'folio_page_artifact_image';

            // Treat images differently.
            $file = folio_file::SelectWhereIdent( $artifact->item_ident );
            if( $file == false ) {
                $title = __gettext('This image has been deleted.');
            } else {
                // Find image size.  If > maxsize, rescale
                list($width, $height, $type, $attr) = getimagesize( $CFG->dataroot . $file->location);

                if( $width > $FOLIO_CFG->maximum_image_width ) {
                    $height = intval(1.0 * $height * $FOLIO_CFG->maximum_image_width  / $width);
                    $width = $FOLIO_CFG->maximum_image_width ;
                }

                // Build link.
                $title = "<a href='{$www}{$view_link}' target='_blank'>" .
                    "<img src='{$www}{$view_link}' alt='{$this->artifact->title}' " .
                    " width='{$width}' height='{$height}' border=0/>" .
                    "</a>";
            }
            $body_text_full = '';
            $mimeImage = '';
        } else {

            $css_class = 'folio_page_artifact_file';

            // Get the image for the filetype
            if( $this->artifact->item_type == 'file' ) {
                $file = folio_file::SelectWhereIdent(
                                $this->artifact->item_ident );
                $file = folio_FileImage::html($file);
            } else {
                $file = folio_FileImage::html(null);
            }

            $title = "<a href='{$www}{$view_link}' target='_blank'>" .
                    "<img src='" . $file . "' border=0/>" .
                    "{$this->artifact->title}</a>";
            /*
            if( strlen( $this->artifact->text ) > 10 ) {
                // Show a little bit of content at the top, and more to expand.
                $body_text .= substr( $this->artifact->text, 0, 10 );
                $body_text_full = substr( $this->artifact->text, 10 );
            } else {
                // Show all content.
                $body_text .= $this->artifact->text;
                $body_text_full = '';
            }
            */
            // For this release, to avoid problems with expanding / contract code breaking up the code in a blog post, just show the entire thing.
            $body_text_full = $this->artifact->text;
        }

    if( $sortable ) {
        // Show editing controls
        $handle_text = "<img id='folio_clip_artifact{$this->artifact->artifact_ident}' src='{$www}mod/folio/image/RichEditor_Move.png' " .
            "alt='{$this->oHandle}' />";
        $delete_text = "<a id='folio_delete_artifact{$this->artifact->artifact_ident}' href=\"javascript:folio_RichEditor_Delete( " .
            "{$this->artifact->artifact_ident},'artifact');\" >" .
            "<img src='{$www}mod/folio/image/RichEditor_Delete.png' alt='{$this->oDelete}" .
            "' class='folio_edit' /></a><br/>" ;
        $reflection = "<div id='{$this->artifact->handle()}_reflection' class='folio_page_editable'>" .
                $this->artifact->reflection . "</div>";
        $li_open = "<li id=\"{$this->artifact->handle()}\" >\n";
        $li_close = "</li>";
    } else {
        // Clean up for final viewing.
        $handle_text = ''; $delete_text = ''; $li_open = ''; $li_close = '';
        if( $this->artifact->reflection == __gettext('Click to reflect on this entry')  ||
            $this->artifact->reflection == __gettext('Reflect on this entry') ) {
            $reflection = '';
        } else {
            $reflection = "<div id='{$this->artifact->handle()}_reflection' class='folio_page_editable'>" .
                $this->artifact->reflection . "</div>";
        }

    }
        $html = <<< END
            {$li_open}
                <span id="{$this->artifact->handle()}_handle" class="handle">{$handle_text}</span>
                {$delete_text}
                <span id="{$this->artifact->handle()}_body" >
                    <div id="{$this->artifact->handle()}_text" class='{$css_class}'>
                        <h3>{$title}</h3>
                        {$body_text_full}
                    </div>
                {$reflection}
                </span>
            {$li_close}
END;
        return $html;

/* Unused css/html for expanding and contracting
<!--                    <span id="{$this->artifact->handle()}_expand"
                        onclick="folio_artifact_ToggleVisible('{$this->artifact->handle()}', true )"
                        >(expand)
                    </span>
                    <br/>
-->
<!-- {$sStatus} -->
<!--                        <span onclick="folio_artifact_ToggleVisible('{$this->artifact->handle()}', false )">(collapse)</span>
-->

        */
    }
}
?>
